package com.smarthome.web.property.controller.statisticsquery;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smarthome.core.housemanager.BuildingManagerService;
import com.smarthome.core.statisticsquery.PropertyFeeStatisticsService;
import com.smarthome.domain.bean.ShPropertyFee;
import com.smarthome.domain.enums.PropertyFeeStatusEnum;
import com.smarthome.domain.vo.boss.Combobox;
import com.smarthome.domain.vo.boss.DataGrid;
import com.smarthome.domain.vo.boss.PageHelper;
import com.smarthome.domain.vo.boss.TimeRange;
import com.smarthome.web.property.utils.UserHolder;

/**
 * 
* @ClassName: PropertyFeeStatisticsController
* @Description: 物业费统计控制类
* @author cyj
* @date 2015年11月17日 下午10:52:41
*
 */
@Controller
@RequestMapping("/propertyFeeStatisticsController")
public class PropertyFeeStatisticsController {

    @Resource
    private PropertyFeeStatisticsService propertyFeeStatisticsService;
    @Resource
    private BuildingManagerService buildingManagerService;

    /**
     * 
    * @Title: dataGrid 
    * @Description: 根据条件以门牌分组获取已缴物业费之和的数据列表 
    * @param propertyFee
    * @param page
    * @param timeRange
    * @return
    * DataGrid
    * @throws 
    * @author cyj
     */
    @RequestMapping("/dataGrid")
    @ResponseBody
    public DataGrid dataGrid(ShPropertyFee propertyFee, PageHelper page,TimeRange timeRange) {
    	if(timeRange.getStartGmtModify() != null) {
    		propertyFee.addQueryData("startGmtModify", timeRange.getStartGmtModify());
    	}
    	if(timeRange.getEndGmtModify() != null) {
    		propertyFee.addQueryData("endGmtModify", timeRange.getEndGmtModify());
    	}
    	if(!UserHolder.isSuperResidence()) {
    		propertyFee.addQueryData("residenceId", UserHolder.getCurrentResidenceId());
    	}
    	propertyFee.setMinAmount(propertyFee.getMinAmount() != null?propertyFee.getMinAmount()*100:null);
    	propertyFee.setMaxAmount(propertyFee.getMaxAmount() != null?propertyFee.getMaxAmount()*100:null);
    	propertyFee.setStatus(PropertyFeeStatusEnum.PAY_YES.getCode());
    	
        return propertyFeeStatisticsService.dataGrid(propertyFee, page);
    }

    
    /**
     * 
    * getResidenceCombobox
    * @Title: getResidenceCombobox
    * @Description: 获取当前小区的楼栋下拉框
    * @return
    * List<Combobox>
    * @throws
    * @author cyj
    * @date 2015年11月18日 上午10:09:54
     */
    @RequestMapping("/buildingCombobox")
    @ResponseBody
	public List<Combobox> getBuildingCombobox(){
    	Long residenceId = UserHolder.getCurrentResidenceId();
		return buildingManagerService.queryComboboxByResidenceId(residenceId);
	}
    
    /**
     * 
    * getAmountSum
    * @Title: getAmountSum
    * @Description: 根据条件获取小区物业费汇总
    * @param timeRange
    * @return
    * Long
    * @throws
    * @author cyj
    * @date 2015年11月18日 下午2:22:33
     */
    @RequestMapping("/getAmountSum")
    @ResponseBody
    public Long getAmountSum(TimeRange timeRange) {
    	ShPropertyFee propertyFee = new ShPropertyFee();
    	if(timeRange.getStartGmtModify() != null) {
    		propertyFee.addQueryData("startGmtModify", timeRange.getStartGmtModify());
    	}
    	if(timeRange.getEndGmtModify() != null) {
    		propertyFee.addQueryData("endGmtModify", timeRange.getEndGmtModify());
    	}
    	if(!UserHolder.isSuperResidence()) {
    		propertyFee.addQueryData("residenceId", UserHolder.getCurrentResidenceId());
    	}
    	propertyFee.setStatus(PropertyFeeStatusEnum.PAY_YES.getCode());
    	Long result = propertyFeeStatisticsService.getAmountSumByResidence(propertyFee);
    	return result != null?result:0;
    }
}
